package com.scau.service.impl;

import com.scau.dao.OrderItemMapper;
import com.scau.dao.cache.RedisDao;
import com.scau.pojo.Goods;
import com.scau.pojo.GoodsExample;
import com.scau.pojo.OrderItem;
import com.scau.pojo.OrderItemExample;
import com.scau.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

/**
 * 数据库order表使用了MySQL关键字
 */
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    RedisDao redisDao;

    @Override
    public List<OrderItem> getBrandOrderItems(String username) {

        OrderItemExample orderExample = new OrderItemExample();
        orderExample.createCriteria().andSellerNameEqualTo(username);

        List<OrderItem> orderList = orderItemMapper.selectByExample(orderExample);
        return orderList;
    }
    @Override
    public OrderItem getBrandOrderItem(String id){
        OrderItem orderItem = redisDao.getOrderItem(id);
        if(orderItem != null) return  orderItem;
        else {
            orderItem = orderItemMapper.selectByPrimaryKey(id);
            redisDao.putOrderItem(orderItem);
            return orderItem;
        }
    }

    @Override
    public int addOrderItem(OrderItem orderItem) {
        //添加事务管理
        orderItemMapper.insertSelective(orderItem);
        redisDao.putOrderItem(orderItem);
        return 0;
    }

    @Override
    public int updateOrderItem(OrderItem orderItem) {
        //添加事务管理
        orderItemMapper.updateByPrimaryKeySelective(orderItem);
        try {
            redisDao.updateOrderItemBySelected(orderItem);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<OrderItem> getOrderItems() {
        OrderItemExample orderExample = new OrderItemExample();
        orderExample.createCriteria().andIdIsNotNull();
        List<OrderItem> orderList = orderItemMapper.selectByExample(orderExample);
        return orderList;
    }

    @Override
    public void deleteOrderItem(String Id) {
        //添加事务管理
        orderItemMapper.deleteByPrimaryKey(Id);
        redisDao.deleteOrderItem(Id);

    }

    @Override
    public List<OrderItem> getBuyerOrderItems(String username) {
        OrderItemExample orderExample = new OrderItemExample();
        orderExample.createCriteria().andBuyerNameEqualTo(username);

        List<OrderItem> orderList = orderItemMapper.selectByExample(orderExample);
        return orderList;
    }
}
